<template>
  <div style="padding: 40px">

    <div style="margin: 10px 0;">
      <el-button type="primary" style="margin-left: 10px;"  v-if="userRole === 'admin' "  @click="add">创建班级</el-button>
    </div>
    <el-table :data="tableData" border stripe style="width: 100%;" header-align="center" align="center">
      <el-table-column label="通知ID" width="70px"  header-align="center" align="center">
        <template slot-scope="scope">
          {{ (currentPage - 1) * pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="班级名称" header-align="center" align="center"/>
      <el-table-column prop="number" label="班级人数" header-align="center" align="center"/>
      <el-table-column prop="introduction" label="班级介绍" header-align="center" align="center"/>
      <el-table-column prop="headmaster" label="指导老师" header-align="center" align="center"/>
      <el-table-column prop="phone" label="联系电话" header-align="center" align="center"/>
      <el-table-column fixed="right" label="操作" width="200" header-align="center" align="center" v-if="userRole === 'admin' ">
        <template #default="scope">
          <!-- 编辑按钮  -->
          <el-button v-if="userRole === 'admin' " type="primary" @click="handleEdit(scope.row)">更新</el-button>

          <!-- 删除按钮 -->
          <el-popconfirm v-if="userRole === 'admin' " title="这段内容确定删除吗?" @onConfirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增弹窗 -->
    <el-dialog :visible.sync="dialogVisible" title="创建班级" width="30%">
      <el-form :model="form" label-width="120px">
        <el-form-item label="班级名称">
          <el-input v-model="form.name" style="width: 50%"></el-input>
        </el-form-item>
        <el-form-item label="班级人数">
          <el-input v-model="form.number" style="width: 50%"></el-input>
        </el-form-item>
        <el-form-item label="班级介绍">
          <el-input v-model="form.introduction" style="width: 50%"></el-input>
        </el-form-item>
        <el-form-item label="指导老师">
          <el-input v-model="form.headmaster" style="width: 50%"></el-input>
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="form.phone" style="width: 50%"></el-input>
        </el-form-item>
      </el-form>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="save">确定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 点击编辑的弹窗 -->
    <el-dialog :visible.sync="dialogVisible1" title="更新班级" width="30%">

      <el-form :model="form" label-width="120px">
        <el-form-item label="班级ID">
          <el-input v-model="form.id" style="width: 50%"></el-input>
        </el-form-item>
        <el-form-item label="班级名称">
          <el-input v-model="form.name" style="width: 50%"></el-input>
        </el-form-item>
        <el-form-item label="班级人数">
          <el-input v-model="form.number" style="width: 50%"></el-input>
        </el-form-item>
        <el-form-item label="班级描述">
          <el-input v-model="form.introduction" style="width: 50%"></el-input>
        </el-form-item>
        <el-form-item label="指导老师">
          <el-input v-model="form.headmaster" style="width: 50%"></el-input>
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="form.phone" style="width: 50%"></el-input>
        </el-form-item>
      </el-form>


      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">取消</el-button>
        <el-button type="primary" @click="saveEdit">确定</el-button>
      </span>

    </el-dialog>

    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="pageSize"
        layout="total,prev, pager, next, jumper"
        :total="total">
    </el-pagination>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import {

  getGuanCreateClassAll, getGCreateClassAll, getGuanDeleteClassDaka, getGClassUpll, getStuCreateClassAl
} from '@/http/userList.js'
import {getGuanliyuanQingAll, getStudenQianDaoAll} from "@/http/categories";


export default {
  name: "Notice",

  data() {
    return {
      form: {
        "id":"",
        "headmaster": "",
        "introduction": "",
        "name": "",
        "number": "",
        "phone": ""
      },


      dialogVisible: false,
      dialogVisible1: false,
      dialogVisible2: false,
      currentPage: 1,
      pageSize: 10,
      total:0,
      tableData: [],

    }

  },
  created() {
    this.load()
  },

  methods: {


    async load() {
      let data = {} // 声明 data 变量

      if (this.userRole === 'admin') {
        const response =  await getGuanCreateClassAll({current: this.currentPage, size: this.pageSize})
        data = response.data
      } else if (this.userRole === 'student') {
        const response = await getStuCreateClassAl({current: this.currentPage, size: this.pageSize})
        data = response.data
      }

      console.log(data)
      this.tableData = data.records || []
      this.total = Number(data.total) || 0
    },
    add() {
      console.log("123");
      this.dialogVisible = true
      this.form = {
        "headmaster": "",
        "introduction": "",
        "name": "",
        "number": "",
        "phone": ""
      }
    },
    // 确认添加
    async save() {
      const {code, message} = await getGCreateClassAll(this.form)
      console.log(this.form)
      if (code == '0') {
        this.$message({
          type: "success",
          message: "添加成功"
        })
      } else {
        this.$message({
          type: "error",
          message: "添加失败"
        })
      }
      this.dialogVisible = false
      this.load()
    },
    // 编辑
    handleEdit(row) {
      console.log(row);
      this.form = { ...row }
      this.dialogVisible1 = true
    },
    // 确认修改
    async saveEdit() {

      const { code, message } = await getGClassUpll(this.form)
      if (code == '0') {
        this.$message({
          type: "success",
          message: "更新成功"
        })
      } else {
        this.$message({
          type: "error",
          message: "更新失败"
        })
      }
      this.dialogVisible1 = false
      this.load()
    },
    async handleDelete(id) {
      console.log("id");
      const { code, message } = await getGuanDeleteClassDaka({ id: id })
      if (code == '0') {
        this.$message({
          type: "success",
          message: "删除成功"
        })
      } else {
        this.$message({
          type: "error",
          message: message
        })
      }
      this.load()  // 删除之后重新加载表格的数据
    },
    handleSizeChange(pageSize ) {
      this.pageSize = pageSize;
      this.currentPage = 1;
      this.load()
    },
    async handleCurrentChange(pageNum) {
      this.currentPage = pageNum
      const { code, message, data } = await getGuanCreateClassAll({ current: pageNum, size: this.pageSize })
      console.log(data)
      this.tableData = data.records
      this.total= Number(data.total)
      this.load(); // 重新加载数据
    },
    onDaloag(row) {
      this.form1 = row
      this.dialogVisible2 = true
    }
  },
  computed: {
    ...mapGetters({
      userRole: 'user/getUserRole'
    })
  }

}
</script>

<style lang="scss" scoped>
.el-table {
  margin-top: 20px;
  
  .el-button {
    padding: 8px 16px;
    margin: 0 4px;
    
    &--danger {
      background: #fff;
      color: var(--danger-color);
      border-color: var(--danger-color);
      
      &:hover {
        color: #fff;
        background: var(--danger-color);
      }
    }
  }
}

.operation-buttons {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 20px;
  
  .el-button {
    display: flex;
    align-items: center;
    gap: 5px;
    
    i {
      font-size: 16px;
    }
  }
}

.el-pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.el-dialog {
  .el-form {
    padding: 20px;
  }
  
  .el-form-item {
    margin-bottom: 20px;
    
    &__label {
      font-weight: 500;
    }
  }
}
</style>
